package com.xj.qq;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.xj.qq.domain.SQlite;
import com.xj.qq.domain.User;
import com.xj.qq.swipe.CustomVideoView;

import java.util.ArrayList;

public class QQLogin extends AppCompatActivity {

    private CustomVideoView videoview;
    private Button btn_enter;
    private Button register;
    private SQlite mSQlite;
    private EditText username;
    private  EditText userpassword;
    private User view;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qq_video);
        btn_enter = (Button) findViewById(R.id.btn_enter);
        register = findViewById(R.id.tv_register);
        username = findViewById(R.id.et_name);
        userpassword = findViewById(R.id.et_pwd);
        initView();
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_r = new Intent(QQLogin.this, Register.class);
                startActivity(intent_r);
                finish();
            }
        });
        btn_enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = username.getText().toString().trim();
                String password = userpassword.getText().toString().trim();
                if(!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)){
                    ArrayList<User> data = mSQlite.getAllDATA();
                    boolean user = false;
                    for(int i = 0; i < data.size(); i++){
                        User userdata = data.get(i);
                        if(name.equals(userdata.getName()) && password.equals(userdata.getPassword())){
                            user = true;
                            break;
                        } else{
                            user = false;
                        }
                    }
                    if(user){
                        Toast.makeText(QQLogin.this, "登录成功", Toast.LENGTH_SHORT).show();
                        Intent intent_l = new Intent(QQLogin.this, MainActivity.class);
                        intent_l.putExtra("username", username.getText().toString());
                        intent_l.putExtra("password", password);
                        startActivity(intent_l);
                        finish();
                    } else{
                        Toast.makeText(QQLogin.this, "用户名或密码错误！", Toast.LENGTH_SHORT).show();
                    }
                } else{
                    Toast.makeText(QQLogin.this, "用户名或密码不能为空！", Toast.LENGTH_SHORT).show();
                }
            }
        });
        mSQlite = new SQlite(QQLogin.this);
    }
    private void initView() {
        videoview = (CustomVideoView) findViewById(R.id.videoview);
        videoview.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.login));
        //播放
        videoview.start();
        //循环播放
        videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                videoview.start();
            }
        });
    }
}
